package com.cls.yygh.hosp.controller;

import com.alibaba.fastjson.JSONObject;
import com.cls.yygh.common.exception.YyghException;
import com.cls.yygh.common.helper.HttpRequestHelper;
import com.cls.yygh.common.result.Result;
import com.cls.yygh.common.result.ResultCodeEnum;
import com.cls.yygh.common.utils.MD5;
import com.cls.yygh.hosp.service.DepartmentService;
import com.cls.yygh.hosp.service.HospitalService;
import com.cls.yygh.hosp.service.HospitalSetService;
import com.cls.yygh.hosp.service.ScheduleService;
import com.cls.yygh.model.hosp.Department;
import com.cls.yygh.model.hosp.Schedule;
import com.cls.yygh.vo.hosp.DepartmentQueryVo;
import com.cls.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: yygh
 * @description:
 * @author: LH
 * @create: 2021-11-08 09:57
 * @Platform: JDK 1.8.0_301
 **/
@Slf4j
@RestController
@Api(tags = "医院管理Api接口")
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiModelProperty(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String sign = (String) paramMap.get("sign");

        String hoscode = (String) paramMap.get("hoscode");

        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("hosname"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("hostype"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("provinceCode"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("cityCode"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("districtCode"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("address"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("logoData"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("intro"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("route"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("bookingRule"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //签名校验
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中"+"转换为了""，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)){
            String replaceAll = logoData.replaceAll(" ", "+");
            paramMap.put("logoData",replaceAll);
        }

        hospitalService.save(paramMap);
        return Result.ok();
    }

    @ApiModelProperty(value = "查询医院")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");

        //参数验证
        if (StringUtils.isEmpty(hoscode)){
            throw new  YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("timestamp"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("sign"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        //签名校验
        if (!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        return Result.ok(hospitalService.getHospitalByHoscode(hoscode));
    }

    @ApiModelProperty(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");

        String sign = (String) paramMap.get("sign");
        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("depcode"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("depcode"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("depname"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("intro"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("bigcode"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("bigname"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("address"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("timestamp"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(sign)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("code"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("message"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("data"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //参数验证
        if (StringUtils.isEmpty(hoscode)){
            throw new  YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        //签名校验
        if (!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);

        return Result.ok();
    }

    @ApiModelProperty(value = "查询科室")
    @PostMapping("department/list")
    public Result departmentList(HttpServletRequest request){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");

//        String sign = (String) paramMap.get("sign");

        //非必填
        String depcode = (String) paramMap.get("depcode");

        if(StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("pageNum"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("pageSize"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("timestamp"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));

        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));


//        String signKey = hospitalSetService.getSignKey(hoscode);


        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page,limit,departmentQueryVo);

        return Result.ok(pageModel);
    }

    @ApiModelProperty(value = "删除科室")
    @PostMapping("department/remove")
    public Result departmentRemove(HttpServletRequest request){

        Map<String,Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String sign = (String) paramMap.get("sign");

        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode,depcode);

        return Result.ok();
    }

    @ApiModelProperty(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String sign = (String) paramMap.get("sign");

        //参数验证
        if(StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(depcode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("title"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("docname"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("skill"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("workDate"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("workTime"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("timestamp"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }if (StringUtils.isEmpty(paramMap.get("sign"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //签名认证
        if (!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);

        return Result.ok();
    }

    @ApiModelProperty(value = "查询排班")
    @PostMapping("schedule/list")
    public Result scheduleList(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");

        //验证参数
        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("timestamp"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(paramMap.get("sign"))){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));

        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode((String) paramMap.get("depcode"));

        Page<Schedule> schedules = scheduleService.selectPage(page, limit, scheduleQueryVo);

        return Result.ok(schedules);
    }

    @ApiModelProperty(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result ScheduleRemove(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        String timestamp = (String) paramMap.get("timestamp");
        String sign = (String) paramMap.get("sign");

        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(hosScheduleId)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(timestamp)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(sign)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode,hosScheduleId);

        return Result.ok();

    }

}
